/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.skydoves.preferenceroomdemo;

import com.skydoves.preferenceroomdemo.entities.Preference_UserProfile;
import com.skydoves.preferenceroomdemo.models.Pet;
import com.skydoves.preferenceroomdemo.models.PrivateInfo;
import junit.framework.TestCase;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;

public class ProfileEntityTest extends TestCase {
    private Preference_UserProfile profile;
    private Preferences preference;

    @Test
    public void testPreference() {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        profile = Preference_UserProfile.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(profile.getEntityName());
        profile.clear();
        preference.putString(profile.nicknameKeyName(), "skydoves").flush();
        preference.putBoolean(profile.LoginKeyName(), false).flush();
        preference.putInt(profile.visitsKeyName(), 1).flush();

        assertThat(
                preference.getString(profile.nicknameKeyName(), null) + "!!!", is(profile.getNickname()));
        assertThat(preference.getBoolean(profile.LoginKeyName(), false), is(profile.getLogin()));
        assertThat(preference.getInt(profile.visitsKeyName(), 1), is(profile.getVisits()));
    }

    @Test
    public void testDefault() throws Exception {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        profile = Preference_UserProfile.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(profile.getEntityName());
        profile.clear();
        assertThat(profile.getNickname(), is("skydoves!!!"));
        assertThat(profile.getLogin(), is(false));
        assertThat(profile.getVisits(), is(1));
        assertThat(profile.getUserinfo().getName(), is("null"));
        Assert.assertNull(profile.getUserPet());
    }

    @Test
    public void testPutPreference() throws Exception {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        profile = Preference_UserProfile.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(profile.getEntityName());
        profile.clear();
        profile.putNickname("PreferenceRoom");
        profile.putLogin(true);
        profile.putVisits(12);
        profile.putUserinfo(new PrivateInfo("Jaewoong", 123));

        assertThat(profile.getNickname(), is("Hello, PreferenceRoom!!!"));
        assertThat(profile.getLogin(), is(true));
        assertThat(profile.getVisits(), is(13));
        assertThat(profile.getUserinfo().getName(), is("Jaewoong"));
        assertThat(profile.getUserinfo().getAge(), is(123));
    }

    @Test
    public void testBaseGsonConverter() throws Exception {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        profile = Preference_UserProfile.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(profile.getEntityName());
        Pet pet = new Pet("skydoves", 11, true, Color.WHITE.getValue());

        profile.putUserPet(pet);
        assertThat(preference.getString(profile.userPetKeyName(), null), notNullValue());

        assertThat(profile.getUserPet().getName(), is("skydoves"));
        assertThat(profile.getUserPet().getAge(), is(11));
        assertThat(profile.getUserPet().isFeed(), is(true));
        assertThat(profile.getUserPet().getColor(), is(Color.WHITE.getValue()));
    }

    @Test
    public void testKeyNameList() throws Exception {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        profile = Preference_UserProfile.getInstance(appContext);
        preference = new DatabaseHelper(appContext).getPreferences(profile.getEntityName());
        Assert.assertEquals(profile.getkeyNameList().size(), 5);
    }
}